<script setup>
import { ref } from 'vue'
import { artManageService } from '@/api/article'
import ManageSelect from '@/components/ManageSelect.vue'
import ManageDrawer from '@/components/ManageDrawer.vue'
import { formatTime } from '@/utils/format.js'
import { Edit, Delete } from '@element-plus/icons-vue'

const loading = ref(false)
const articleList = ref([])
const drawer = ref()
const total = ref(0) //文章总数
const params = ref({
  pagenum: 1,
  pagesize: 5,
  cate_id: '',
  state: ''
})

const getManageList = async () => {
  loading.value = true
  const res = await artManageService(params.value)
  articleList.value = res.data.data
  total.value = res.data.total
  loading.value = false
}

getManageList()

const onPublish = () => {
  drawer.value.open({})
}
const onSuccess = (type) => {
  if (type === 'add') {
    const lastPage = Math.ceil((total.value + 1) / params.value.pagesize)
    params.value.pagenum = lastPage
  }
  getManageList()
}
const onEditManage = ($index, row) => {
  drawer.value.open(row)
}
const onDeleteManage = ($index, row) => {
  console.log(row)
}

const handleSizeChange = (val) => {
  params.value.pagenum = 1
  params.value.pagesize = val
  getManageList()
}
const handleCurrentChange = (val) => {
  params.value.pagenum = val
  getManageList()
}

const onSearch = () => {
  params.value.pagenum = 1
  getManageList()
}
const onReset = () => {
  params.value.pagenum = 1
  params.value.cate_id = ''
  params.value.state = ''
  getManageList()
}
</script>
<template>
  <PageContainer title="文章管理">
    <template #extra>
      <el-button @click="onPublish" type="primary">发布文章</el-button>
    </template>
    <div class="main-input">
      <el-form inline>
        <el-form-item label="文章分类：" style="width: 300px">
          <ManageSelect
            v-model="params.cate_id"
            style="width: 300px"
          ></ManageSelect>
        </el-form-item>

        <el-form-item label="发布状态：" style="width: 300px">
          <el-select placeholder="请选择">
            <el-option label="已发布" value="shanghai" />
            <el-option label="草稿" value="beijing" />
          </el-select>
        </el-form-item>
      </el-form>
      <el-form-item style="margin-left: 10px">
        <el-button @click="onSearch" type="primary">搜索</el-button>
        <el-button @click="onReset">重置</el-button>
      </el-form-item>
    </div>
    <el-table
      :data="articleList"
      v-loading="loading"
      height="400"
      style="width: 100%"
    >
      <el-table-column label="文章标题" width="200">
        <template #default="{ row }">
          <el-link type="primary" :underline="false">{{ row.title }}</el-link>
        </template>
      </el-table-column>
      <el-table-column prop="cate_name" label="分类" width="100" />
      <el-table-column prop="pub_date" label="发表时间">
        <template #default="{ row }">
          {{ formatTime(row.pub_date) }}
        </template>
      </el-table-column>
      <el-table-column prop="state" label="状态" width="100" />
      <el-table-column label="操作" width="150">
        <template #default="scope">
          <el-button
            type="primary"
            @click="onEditManage(scope.$index, scope.row)"
            :icon="Edit"
            circle
          />
          <el-button
            type="danger"
            @click="onDeleteManage(scope.$index, scope.row)"
            :icon="Delete"
            circle
          />
        </template>
      </el-table-column>
      <template #empty>
        <el-empty description="没有数据" />
      </template>
    </el-table>
    <div class="main-footer">
      <el-pagination
        v-model:current-page="params.pagenum"
        v-model:page-size="params.pagesize"
        :page-sizes="[2, 4, 5, 10]"
        background="true"
        layout="jumper, total, sizes, prev, pager, next"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>

    <ManageDrawer ref="drawer" @success="onSuccess"></ManageDrawer>
  </PageContainer>
</template>
<style scoped>
.main-input {
  width: 100%;
  display: flex;
  align-items: flex-start;
}
.el-card__body {
  height: 500px;
}
.main-footer {
  display: flex;
  justify-content: end;
  margin-top: 10px;
}
</style>
